/**
 * 
 */
package com.shy.freemarker.directive;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.shy.cms.entity.Site;
import com.shy.cms.service.SiteService;
import com.shy.freemarker.util.DirectiveUtils;

import freemarker.core.Environment;
import freemarker.template.AdapterTemplateModel;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * <p>Title:BaseDirective </p>
 * <p>Description: </p>
 * <p>Company:ipebg </p> 
 * @author H2013788
 * @date 2019年8月15日
 */
@Component
public abstract class BaseDirective implements TemplateDirectiveModel{

	/**
	 * 输入参数，站点ID。可选。允许多个站点ID，用","分开。
	 */
	public static final String PARAM_SITE_ID = "siteid";
	
	/**
	 * 输入参数，类型ID。可选。允许多个类型ID,用","分开。
	 */
	public static final String PARAM_TYPE_ID = "typeid";
	
	/**
	 * 输入参数，排序值。可选
	 */
	public static final String PARAM_ORDER_BY = "orderBy";
	
	
	/**
	 * 站点
	 */
	public static final String SITE = "site";
	
	
	
	/**
	 * 查询站点信息
	 * @param params
	 * @param env
	 * @throws TemplateException
	 * @author H2013788
	 * @date 2019年8月15日
	 */
	protected Site getSiteData(Map<String, TemplateModel> params)
			throws TemplateException {
		Integer siteid = DirectiveUtils.getInt(PARAM_SITE_ID, params);
		if(null != siteid) {
			Site site = siteService.getById(siteid);
			return site;
		}
		return null;
	}
	
	
	/**
	 * 标签中获得站点
	 * 
	 * @param env
	 * @return
	 * @throws TemplateModelException
	 */
	public static Site getSite(Environment env)
			throws TemplateModelException {
		TemplateModel model = env.getGlobalVariable(SITE);
		if (null != model && model instanceof AdapterTemplateModel) {
			return (Site) ((AdapterTemplateModel) model)
					.getAdaptedObject(Site.class);
		} else {
			//throw new TemplateModelException("'" + SITE
			//		+ "' not found in DataModel");
			return null;
		}
	}
	
	@Resource
	private SiteService siteService;
	
	
}
